package zp.weibo;

import java.util.List;

import zp.weibo.util.Message;
import zp.weibo.util.WeiboUtil;
import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.widget.RemoteViews;
import android.widget.Toast;

public class WeiboProvider extends AppWidgetProvider {

	/**
	 * 获取到的微博列表
	 */
	private static List<Message> msgs;

	/**
	 * 当前所显示微博消息编号
	 */
	private static int cur = 0;

	private static RemoteViews views;

	private static ComponentName widget;

	/**
	 * 消息点击字串
	 */
	// private static String CLICK_CONTENT_ACTION = "zp.weibo.content.click";
	/**
	 * 查看前面一条消息点击字串
	 */
	private static String CLICK_LEFT_ACTION = "zp.weibo.left.click";

	/**
	 * 查看后面一条消息点击字串
	 */
	private static String CLICK_RIGHT_ACTION = "zp.weibo.right.click";

	@Override
	public void onReceive(Context context, Intent intent) {
		// TODO Auto-generated method stub
		super.onReceive(context, intent);

		views = new RemoteViews(context.getPackageName(), R.layout.weiboyout);

		if (null != widget)
			widget = new ComponentName(context, this.getClass());
		// 当按下记录时触发
		// if (intent.getAction().equals(CLICK_CONTENT_ACTION)) {
		//
		// Toast.makeText(context, "content", Toast.LENGTH_SHORT).show();
		//
		// // 进入新的activity
		// Context mContext = context;
		// views = new RemoteViews(context.getPackageName(), R.layout.main);
		// // Calendar cal=Calendar.getInstance();
		// Intent in = new Intent();
		// in.setClass(mContext, WeiboActivity.class);
		// Bundle mBundle = new Bundle();
		// mBundle.putSerializable("msgs", (Serializable) msgs);
		// mBundle.putInt("cur", cur);
		// in.putExtras(mBundle);
		//
		//
		// }
		// 当按下LEFT时触发
		if (intent.getAction().equals(CLICK_LEFT_ACTION)) {
			if (cur > 0) {
				cur--;
				views.setTextViewText(R.id.content, getCurConent());
			} else {
				Toast.makeText(context, "前面没有了哦！", Toast.LENGTH_SHORT).show();
			}

		}

		// 当按下RIGHT时触发
		if (intent.getAction().equals(CLICK_RIGHT_ACTION)) {
			int max = msgs.size();
			if (cur < max - 1) {
				cur++;
				views.setTextViewText(R.id.content, getCurConent());
			} else {
				Toast.makeText(context, "到底了哦！", Toast.LENGTH_SHORT).show();
			}

		}
		AppWidgetManager appWidgetManager = AppWidgetManager
				.getInstance(context);

		appWidgetManager.updateAppWidget(widget, views);

	}

	@Override
	public void onUpdate(Context context, AppWidgetManager appWidgetManager,
			int[] appWidgetIds) {
		// TODO Auto-generated method stub
		super.onUpdate(context, appWidgetManager, appWidgetIds);

		try {
			msgs = WeiboUtil.getContext();
			cur = 0;
		} catch (Exception e) {
			Toast.makeText(context, "获取内容出错！", Toast.LENGTH_SHORT).show();
		}

		// 添加点击事件
		addClickListener(context);

		if (null != widget)
			widget = new ComponentName(context, this.getClass());

		views = new RemoteViews(context.getPackageName(), R.layout.weiboyout);

		views.setTextViewText(R.id.content, getCurConent());

		appWidgetManager.updateAppWidget(widget, views);


	}

	/**
	 * 添加点击事件
	 * 
	 * @param context
	 */
	private void addClickListener(Context context) {

		if (null == widget)
			widget = new ComponentName(context, this.getClass());
		views = new RemoteViews(context.getPackageName(), R.layout.weiboyout);


		addContentListener(context,views);
		
		// 点击left
		Intent intentClick = new Intent(CLICK_LEFT_ACTION);
		PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0,
				intentClick, 0);
		views.setOnClickPendingIntent(R.id.left, pendingIntent);

		// 点击right
		intentClick = new Intent(CLICK_RIGHT_ACTION);
		pendingIntent = PendingIntent.getBroadcast(context, 0, intentClick, 0);
		views.setOnClickPendingIntent(R.id.right, pendingIntent);

		// 更新widget
		AppWidgetManager appWidgetManager = AppWidgetManager
				.getInstance(context);
		appWidgetManager.updateAppWidget(widget, views);


	}

	private void addContentListener(Context context, RemoteViews rv) {
		if (null == widget)
			widget = new ComponentName(context, this.getClass());
		if (rv == null)
			rv = new RemoteViews(context.getPackageName(),
					R.layout.weiboyout);

		// 点击博客

		Intent intentClick = new Intent(context, WeiboActivity.class);
//		Bundle mBundle = new Bundle();
//		mBundle.putSerializable("msgs",  (Serializable)msgs);
//		Test test = new Test();
//		mBundle.putSerializable("test", test);
//		mBundle.putInt("cur", cur);
//		intentClick.putExtras(mBundle);

		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
				intentClick, 0);
		rv.setOnClickPendingIntent(R.id.content, pendingIntent);

		// 更新widget
//		AppWidgetManager appWidgetManager = AppWidgetManager
//				.getInstance(context);
//		appWidgetManager.updateAppWidget(widget, views);
	}

	/**
	 * 获取当前记录
	 * 
	 * @return
	 */
	private String getCurConent() {
		try {
			Message msg = msgs.get(cur);
			String content = msg.getUser() + "\n" + msg.getContent().substring(0, 30);
			return content;
		} catch (Exception e) {
			return "No Message!";
		}

	}

}